// This is a part of the Microsoft Foundation Classes C++ library. 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.

// ChildView.h : interface of the CChildView class
//


#pragma once
#include <afxstr.h>
#include <atlimage.h>
#include <comdef.h>
#include <Icm.h>
#include "BasicFunction.h"
#include "ImageLab.h"
#include "Morphological.h"
using namespace std;

struct luv
{
	double	luvL;
	double	luvU;
	double	luvV;
};
struct hsi
{
	double	hsiH;
	double	hsiS;
	double	hsiI;
};
struct region
{
	int		top;
	int		left;
	int		bottom;
	int		right;
	int		centrex;
	int		centrey;
	int		sequence;
	int		reg_size;
	double* colour;
	double* shape;
	double* edge;
	double* texture;
	bool	merged;
};

// CChildView window

class CChildView : public CWnd
{
// Construction
public:
	CChildView();

// Attributes
public:

// Operations
public:
	
// Overrides
	protected:
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

// Implementation
public:
	virtual ~CChildView();

	// Generated message map functions

private:
	int				view_x, view_y, view_dx, view_dy;		//client information for display
	int				image_x, image_y, image_dx, image_dy;	//image region for display
	CPtrArray		c_ptr;									//pointer to image
	CPtrArray		c_region;								//pointer to regions
	CPtrArray		c_index;								//pointer to index
	rgb**			lpoha;									//pointer to image
	rgb**			image;									//pointer to image
	byte**			grey;									//pointer to gray image
	byte**			grey_ave;								//pointer to average gray image
	int**			index;									//pointer to index
	rgb**			lpohatemp;								//pointer to temporary image
	byte**			grey_temp;								//pointer to temporary gray image
	bool**			img_binary;								//a binary image;
	bool**			img_binary_temp;						//temporal binary image;

	int				m_nFilterLoad;							
public:
	int				width;									//image width
	int				height;									//image height
private:
	CString			imagePath;								//image path
	CString			imageName;								//image name
	bool			isSegmented;							//flag for segmentation
	int				regSelected;							//flag for region selection
	CStringArray	similarImages;							//image names for similarity
	bool			isSegStore;								//flag for region store

	CScrollBar		h_scrollbar;							//horizontal scrollbar
	CScrollBar		v_scrollbar;							//vertical scrollbar

	bool			h_scroll_created;						//is horizontal scrollbar created
	bool			v_scroll_created;						//is vertical scrollbar created

	bool			scroll_h;								//flag for h scrollbar
	bool			scroll_v;								//flag for v scrollbar
	bool			scroll_both;							//flag for both scrollbar
	bool			x_resize;								//flag for x resize;
	bool			y_resize;								//flag for y resize;
	CBasicFunction	bf;										//basic function
	bool			is_grey;								//status for if image is convert to grey?
	bool			is_binary;								//status for if the image is binary?

	int				CurrentNum;								//a global variable for indicating the 
															//current sequence of the panel
	int				leftmouse;								//status for indicating if left mouse function

	WCHAR			m_ImageFolder[256];						//folder pointer to the images
	char**			m_ImageFiles;							//a pointer of the images in the folder
	int				m_CurImage;								//current poisition of the images
	int				m_NumImage;								//number of images found
	bool			m_IsFolderLoaded;							//indicator for loading images

	int				m_CurRed;								//red of the current point
	int				m_CurGreen;								//green of the current point
	int				m_CurBlue;								//blue of the current point

	CStringA		m_ColourInfo;							//colour information to be display in the pane

	bool			m_IsResize;								//Indicator if the image is resize or not
	int				resizeX;
	int				resizeY;

	int				m_Round;								//Rounds of the experiments

	CString			m_CurrentFolder;						//Folder for batch process

	bool			m_Normalized;							//Normalize or not

public:
	CImage			imgOriginal;							//the image view
	CImage			imgResize;								//an resize image
	CMorphological*	img_mor;								//create a point to the morphological dialog

protected:
	afx_msg void OnPaint();		//paint the client area
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);	//on left bottom down
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);	//on mouse move over
	afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar);	//on vertical scroll
	afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar *pScrollBar);	//on horizontal scroll
	DECLARE_MESSAGE_MAP()
public:
//operations
	bool		OnImageOpen(CString filepath, CString filename);	//open an image with filepath and filename
	void		OnFileOpenimage(void);	//open an image with dialog
	void		OnFileSaveImage(void);	//save an image
	void		OnTest(void);	//test function
	void		OnImageInfo(void);
	void		OnUpdateImage(CCmdUI* pCmdUI);
	void		Destroy(void);
	void		CleanOldMemory(void);	//clean the old memory for old image
	void		LocateNewMemory(void);	//locate memory for open an new image
	void		OnNewImage(void);		//create a new image
	CString		getImageName(void);		//get the name of the image
//image filters
	void		MedianFilter(void);		//median filter
	void		OnLawFilter(void);		//law's filter
	void		OnMedianFilter(byte** m_OrginalImage, int m_KernelX, int m_KernekY, byte** m_MedianImage);	//get the median filtered image
	void		OnMedianFilter(rgb** m_OrginalImage, int m_KernelX, int m_KernekY, rgb** m_MedianImage);	//get the median filtered image
	void		CustomFilter(void);		//filter the image using custom designed filter
//image segmentation
	void		setRegions(void);		//set the regions for computer to read
	void		Otsu2d(void);			//otsu 2d thresholds
	void		Otsu1d(void);			//otsu 1d thresholds
	void		BatchImage(void);		//process images in a folder
	void		OnImageSelfOrg(void);	//segment images with HSOM
	double		otsuThreshold(double* hist, int histSize);	//otsu 1d threshold
	double		entropyThreshold(double* hist, int histSize);	//karpur 1d threshold
	void		otsuThreshold2d(double** hist, int histSiz_x, int histSize_y, int* threshold_x, int* threshold_y); //otsu 2d threshold
	void		GraphicSOM(void);
	void		SingleSOMRGB(void);	//image segmentation using single layer SOM
	void		OnSemiManualCut(void);//cut the image panels semi mannually
	void		OnGrowToWhite(int x, int y, int* RegionLeft, int* RegionRight, int* RegionTop,
								int* RegionBottom);
	void		OnGetGray(void);
	void		OnSOMSVD(void);	//image segmentation using SOM and SVD
	void		OnRandomSOMSVD(void);	//image segmentation using random svd and som
	void		OnWaveletSOM(void);		//SOM for segmentation using wavelet features
	void		OnSVDThresholding(void);	//Combining SVD with thresholding
//basic functions
	CString		douTostr(double dou);
	long		factorial(int num);
	double*		stupidMicrosoft(void);
	bool		normalizedData(double** un_data, int un_r, int un_c, double* n_ave, double* n_std);
//region growing
	void		OnRegionGrow(void);
//obtain image features
	double*		OnColourMoment(int left, int right, int top, int bottom, int reg_index);
	void		GetImageFeature(void);
	double*		OnImageColour(int left, int right, int top, int bottom, int reg_index);
	double*		OnZernikeMoment(int left, int right, int top, int bottom, int reg_index);
	double*		OnImageTexture(int left, int right, int top, int bottom, int reg_index);
	double*		OnSpatialMoment(int left, int right, int top, int bottom, int reg_index);	// computer the spatial moment
	double*		OnLawTexture(int left, int right, int top, int bottom, int reg_index);		// computer the texture using law's filter
	int			getZernike(double** i_matrix, int i_X, int i_Y, int n, int m, double** f_zernike);				// computer the zernike moment features
	int			getZernikeNum(int n, int m);
	double		getRadialPoly(double p, int n, int m);
	double**	getZernikeImage(int n, int m);
	int			GetImageHeight();
	int			GetImageWidth();
	void		GetFolderFeature();
	double*		GetHSOMFeature(int* NumFeature);
	void		OnRegionGrowing(void);
	vector<vector<double>> GetImageWaveletFeature(unsigned int numberofscale, unsigned int zernikemomentm,
											unsigned int zernikemomentn);

//image self organizing map
	void		somSetregion(void);
	bool		somFilter(void);
//image retrieval
	void		OnSearchImage(void);
//colour space transfer
	luv			RGB2LUV(rgb rgbcolour);
	hsi			RGB2HSI(rgb rgbcolour);
//region operator
	double		regDistance(region* reg1, region* reg2, int numColour, int numShape, int numTexture);
	void		regMerging(region* mergingReg, region* mergedReg);
	bool		stringToFile(CString str);
	void		pixelFeature(void);
	void		RegionGrowTex(int startx, int starty, int endx, int endy);
//colour channel
	void		OnRedChannel(void);
	void		OnGreenChannel(void);
	void		OnBlueChannel(void);
	void		OnGrayChannel(void);
	void		OnRecover(void);
//plot an binary image
	void		PlotBinary(int* m_index, int m_length);
//mophological operators
	void		OnImageBinary(void);
	void		ImageErode(void);
	void		ImageDialate(void);
	void		OnImageClosing(void);
	void		OnImageMorphological(void);
	void		OnImageInvert(void);
	void		OnCombineBinary(void);
	void		OnPlotEdge(void);
//transformation
	//image resize
	CImage*		OnImageResize(int new_w, int new_h);
	//convert an image to 24 bit
	void		OnConvert24Bit();
	//Wavelet transform
	void		OnWaveletTransform(void);
	//Resize the image
	void		OnImageResizeControl(void);

//batch process
	void		OnLoadImageFolder(void);		//load a folder of images
	void		OnMoveNext(void);				//move to the next images
	void		OnMovePrevious(void);			//move to the previous images
	void		OnUnloadFolder(void);			//unload the folder of images
//Other functions
	int			OnVoting(int* m_VotingData, int m_NumberVote, int m_DataLength);
// Scales an image to fit into an imaginary rectangle of width x and height y without distorting proportions
	void		CChildView::ResizeImage(int x, int y, bool resizeflag);

//SOM and SVD
//Batch process the images using somsvd
	void		BatchSOMSVD(void);
//Batch process the images using hsom
	void		BatchHSOM(void);
//process the image using single som with random svd
	void		SingleSOMRandomSVD(void);
//process the image using single som with power 2 svd
	void		OnSingleSOMSVD(void);

//Image classification
	void		OnImageClassification(void);

//DICOM image
	void		OpenDICOMFile(CString filepath);
	void		OpenDICOM(void);
	void		OpenSetOfDICOM(void);
};
